package com.itclass.auth.util;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.Set;

@Slf4j
public class HttpClientUtils {




    public static String doPost(String url, String data,  Map<String, String> headers,String contentType) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
        httpPost.setConfig(config);
        HttpEntity entity = new StringEntity(data, "utf-8");
        httpPost.setEntity(entity);
        return sendRequest(httpClient, httpPost, contentType,headers);
    }
    public static String doPost(String url, String data, Map<String, String> headers) {
        return doPost(url, data, headers,null);
    }
    public static String doPost(String url, String data, String contentType) {
        return doPost(url, data, null,contentType);
    }
    public static String doPost(String url, String data) {
        return doPost(url, data, MediaType.APPLICATION_JSON_VALUE);
    }
    private static String sendRequest(CloseableHttpClient httpClient, HttpUriRequest request, String contentType, Map<String, String> headers) {
        String content = null;
        CloseableHttpResponse response = null;
        try {
            request.setHeader("Content-Type", contentType);
            if (headers != null && headers.size() > 0) {
                Set<String> strings = headers.keySet();
                for (String key : strings) {
                    request.setHeader(key, headers.get(key));
                }
            }
            response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity, "utf-8");

        } catch (ClientProtocolException e) {
            log.error("http request error.uri:" + request.getURI(), e);
            throw new RuntimeException(e.getMessage());
        } catch (ConnectTimeoutException e) {
            log.error("http request connect time out.uri:" + request.getURI(), e);
            throw new RuntimeException(e.getMessage());
        } catch (SocketTimeoutException e) {
            log.error("http request socket time out.uri:" + request.getURI(), e);
            throw new RuntimeException(e.getMessage());
        } catch (Exception e) {
            log.error("http request error.uri:" + request.getURI(), e);
            throw new RuntimeException(e.getMessage());
        } finally {
            try {
                if (response != null) response.close();
            } catch (IOException e) {
                log.error("close stream error", e);
            }
            try {
                if (httpClient != null) httpClient.close();
            } catch (IOException e) {
                log.error("close stream error", e);
            }
        }
        return content;
    }
}
